/*****************************************************************
Mix3D WebGL Game Engine
Copyright (c) fantasy ( fantasy.x2x@gmail.com )
Listens:
Distributed under the Boost Software License, Version 1.0.
http://www.boost.org/LICENSE_1_0.txt
 ****************************************************************/
/**
 * @Desc    : float array
 * @Author  : fantasy(fantasy.x2x@gmail.com)
 */
//////////////////////////////////////////////////////////////////
/*defined namespace */
if(typeof(Mix3D) === 'undefined')
{
  Mix3D = {};
}
(function(Mix3D){
//////////////////////////////////////////////////////////////////
/**
 * @desc : define engine used float array type;
 **/
Mix3D.FloatArrayType = (typeof Float32Array!="undefined")?Float32Array:
                        (typeof WebGLFloatArray!="undefined")?WebGLFloatArray:Array;

//////////////////////////////////////////////////////////////////
///
/// FloatArray
///
//////////////////////////////////////////////////////////////////
Mix3D.FloatArray = function(dim)
{
  this.FA = (dim!==undefined) ? new Mix3D.FloatArrayType(dim) : undefined;
};

Mix3D.FloatArray.prototype.getArraySize = function() { return this.FA.length; };

Mix3D.FloatArray.prototype.resize = function( newLen, v )
{
  if(newLen===undefined)newLen=0;
  if(v===undefined)v=0;
  
  var len = this.FA.length;
  if(len > newLen)
  {
    this.FA.splice(newLen, len-newLen);
  }
  var i;
  for( i=0; i<len; ++i)
  {
    this.FA[i]=v;
  }
  for( i=len; i<newLen; ++i)
  {
    this.FA.push(0);
  }
};

Mix3D.FloatArray.prototype.isEquals = function( other )
{
  for(var i=0, len=this.FA.length; i<len; ++i)
  {
    if(this.FA[i] !== other.FA[i])return false;
  }
  return true;
};

Mix3D.FloatArray.prototype.isSameAs = function( other )
{
  for(var i=0, len=this.FA.length; i<len; ++i)
  {
    if(Math.abs(other.FA[i]-this.FA[i])>Mix3D.Math.Epsilon )return false;
  }
  return true;
};

Mix3D.FloatArray.prototype.isZero = function()
{
  var total=0;
  for(var i=0, len=this.FA.length; i<len; ++i)
    total += this.FA[i];
  return (total < Mix3D.Math.Epsilon );
};

Mix3D.FloatArray.prototype.minValue = function()
{
  var min = Mix3D.Math.POS_INF;
  for(var i=0, len=this.FA.length; i<len; ++i)
  {
    if(this.FA[i] < min)min = tis.FA[i];
  }
  return min;
};

Mix3D.FloatArray.prototype.maxValue = function()
{
  var max = Mix3D.Math.NEG_INF;
  for(var i=0, len=this.FA.length; i<len; ++i)
  {
    if(this.FA[i] > max)max = tis.FA[i];
  }
  return max;
};

Mix3D.FloatArray.prototype.copy = function( other )
{
  for(var i=0, len=this.FA.length; i<len; ++i)
    this.FA[i] = other.FA[i];
  return this;
}; 

Mix3D.FloatArray.prototype.clone = function()
{
  var fa = new Mix3D.FloatArray(this.FA.length);
  return fa.copy(this);
};

Mix3D.FloatArray.prototype.toArray = function( outArray, offset)
{
  if(outArray === undefined) 
  {
    outArray = [];
    offset = 0;
  }
  if(offset===undefined)offset=0;
  for(var i=0,len=this.FA.length;i<len;++i)
  {
    outArray[offset++]=this.FA[i];
  }
  return outArray;
};

Mix3D.FloatArray.prototype.fromArray = function( inArray, offset)
{
  if(!inArray)return;
  var i, len = this.FA.length;
  if(offset===undefined)offset=0;
  var copyLen = inArray.length - offset;
  
  if( len > copyLen)
  {
    this.FA.splice(copyLen, len-copyLen);
    len = copyLen;
  }
  
  for(i=0; i<len; ++i)
  {
    this.FA[i] = inArray[offset++];
  }
  for( i=len; i<copyLen; ++i)
  {
    this.FA.push(inArray[offset++]);
  }
  return this;
};

Mix3D.FloatArray.prototype.toStr = function( outString )
{
  var i,len=this.FA.length-1;
  if(outString === undefined)outString='';
  outString += "[";
  for( i=0; i<len; ++i)
  {
    outString += this.FA[i] + ', ';
  }
  outString += this.FA[len];
  outString +="]";
  return outString;
};
//////////////////////////////////////////////////////////////////
})(Mix3D);//endof function(Mix3D)